(function($) {

	app = $.sammy('#content', function() {

		var surveysView = new $.fn.SurveysView();

		// routes
		//***************************************************
		this.get('#/', function(context) {
			// get all stati
			var asStati =
			[
			 	"active", "inactive", "archived", "stopped", "incomplete"
			];
			getSurveyList(asStati, context, false);
		});

		this.get('#/survey/:id', function(context) {
			$.ajax({
				url:'surveys.php?method=getSurvey&id=' + this.params['id'],
				dataType: 'json',
				async: false,
				success: function(oResponse) {
					if(oResponse.sStatus === "action_unsuccessfull") {
						surveysView.showSurveyError(context, oResponse.sReason);
					} else {
						surveysView.showSurvey(context, oResponse.oSurvey);
					}
				}
			});
		});

		this.get('#/new', function(context) {
			$.ajax({
				url:'surveys.php?method=getNewSurvey',
				dataType: 'json',
				async: false,
				success: function(oResponse) {
					if(oResponse.sStatus === "action_unsuccessfull") {
						surveysView.showSurveyError(context, oResponse.sReason);
					} else {
						surveysView.showSurvey(context, oResponse.oSurvey);
					}
				}
			});
		});

		this.get('#/result/:id', function(context) {
			$.ajax({
				url:'surveys.php?method=getResult&id=' + this.params['id'],
				dataType: 'json',
				async: false,
				success: function(oResponse) {
					if(oResponse.sStatus === "action_unsuccessfull") {
						$('.appStatus').text(oResponse.sReason)
						.addClass("ui-state-error");
					} else {
					}
				}
			});
		});

		this.post('#/', function(context) {
			// get all stati
			var asStati = [];
			$('input.statusCheckbox').each(function() {
				if($(this).attr("checked"))
					asStati.push($(this).attr("name"));
			});

			getSurveyList(asStati, context, true);
		});

		this.post('#/survey/:_id', function(context) {
			$.ajax({
				url:'surveys.php?method=save&_id=' + this.params['_id'],
				dataType: 'json',
				data: this.params,
				async: false,
				success: function(oResponse) {
					if(oResponse.sStatus === "action_successfull") {
						surveysView.showSurvey(context, oResponse.oSurvey);
						$('#surveyStatus').text("Gespeichert!")
						.removeClass("ui-state-error")
						.addClass("ui-state-highlight");
					} else {
						$('#surveyStatus').text("Speichern fehlgeschlagen: " + oResponse.sReason)
						.removeClass("ui-state-highlight")
						.addClass("ui-state-error");
					}
				}
			});
		});

		/**
		 * 
		 */
		getSurveyList = function(asStati, context, bRefresh) {
			$.ajax({
				url:'surveys.php?method=getSurveys',
				dataType: 'json',
				data: "asStati=" + JSON.stringify(asStati),
				async: false,
				success: function(oResponse) {
				    if(bRefresh) {
					    surveysView.refreshSurveysTable(oResponse, context); // view init
					} else {
					    surveysView.showSurveyList(oResponse, context); // view 
					}
					if(oResponse.sStatus === "action_unsuccessfull") {
						$('.appStatus').text("Fehler beim Laden: " + oResponse.sReason)
						.removeClass("ui-state-highlight")
						.addClass("ui-state-error");
					}
				}
			});
		};
	});

	$(function() {
		app.run('#/');
	});

})(jQuery);
